package com.guet.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.guet.DTO.DocumentDTO;
import com.guet.DTO.DocumentPageDTO;
import com.guet.DTO.VideoDTO;
import com.guet.Entity.Document;
import com.guet.Entity.Video;
import com.guet.VO.DocumentVO;
import com.guet.context.BaseContext;
import com.guet.mapper.DocumentMapper;
import com.guet.mapper.VideoMapper;
import com.guet.result.PageResult;
import com.guet.service.DocumentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DocumentServiceImpl implements DocumentService {
    @Autowired
    private DocumentMapper documentMapper;

    /**
     * 添加文档
     *
     * @param documentDTO
     */
    @Override
    public void addDocument(DocumentDTO documentDTO) {
        Long userId = BaseContext.getCurrentId();
        Document document = new Document();
        BeanUtils.copyProperties(documentDTO, document);
        document.setUserId(userId);
        document.setCreateTime(LocalDateTime.now());
        documentMapper.insert(document);
    }

    /**
     * 分页查询文档
     *
     * @param documentPageDTO
     * @return
     */
    @Override
    public PageResult list(DocumentPageDTO documentPageDTO) {
        PageHelper.startPage(documentPageDTO.getCurrentPage(), documentPageDTO.getPageSize());
        Page<DocumentVO> result = documentMapper.list(documentPageDTO);
        Long total = result.getTotal();
        List<DocumentVO> records = result.getResult();
        return new PageResult(total, records);
    }

    /**
     * 获取文档详情
     *
     * @param id
     * @return
     */
    @Override
    public DocumentVO queryById(Integer id) {
        return documentMapper.queryById(id);
    }

    @Override
    public void deleteById(Integer id) {
        documentMapper.deleteById(id);
    }
}
